using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using XPTable;
using XPTable.Editors;
using XPTable.Models;

namespace View
{
	/// <summary>
	/// Summary description for AddPayeeDialog.
	/// </summary>
	public class NewUser : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private PinkieControls.ButtonXP cancelButton;
		private PinkieControls.ButtonXP addButton;
		private XPTable.Models.Table payeesTable;
		private System.Windows.Forms.Label totalAmountLabel;
		private System.Windows.Forms.TextBox passwordField;
		private System.Windows.Forms.TextBox confirmPasswordField;
		private System.Windows.Forms.TextBox usernameField;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private Model.UserManager manager;
		private Model.UserIdentity newUser = new Model.UserIdentity();

		public NewUser(Model.UserManager m)
		{

			manager = m;

			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.passwordField = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.confirmPasswordField = new System.Windows.Forms.TextBox();
			this.cancelButton = new PinkieControls.ButtonXP();
			this.addButton = new PinkieControls.ButtonXP();
			this.usernameField = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Username:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "Password:";
			// 
			// passwordField
			// 
			this.passwordField.Location = new System.Drawing.Point(112, 64);
			this.passwordField.Name = "passwordField";
			this.passwordField.PasswordChar = '*';
			this.passwordField.Size = new System.Drawing.Size(152, 20);
			this.passwordField.TabIndex = 3;
			this.passwordField.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 96);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(104, 23);
			this.label3.TabIndex = 4;
			this.label3.Text = "Confirm password:";
			// 
			// confirmPasswordField
			// 
			this.confirmPasswordField.Location = new System.Drawing.Point(112, 96);
			this.confirmPasswordField.Name = "confirmPasswordField";
			this.confirmPasswordField.PasswordChar = '*';
			this.confirmPasswordField.Size = new System.Drawing.Size(152, 20);
			this.confirmPasswordField.TabIndex = 5;
			this.confirmPasswordField.Text = "";
			this.confirmPasswordField.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Enter_KeyUp);
			// 
			// cancelButton
			// 
			this.cancelButton.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(224)), ((System.Byte)(223)), ((System.Byte)(227)));
			this.cancelButton.DefaultScheme = true;
			this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.None;
			this.cancelButton.Hint = "";
			this.cancelButton.Location = new System.Drawing.Point(149, 152);
			this.cancelButton.Name = "cancelButton";
			this.cancelButton.Scheme = PinkieControls.ButtonXP.Schemes.Silver;
			this.cancelButton.Size = new System.Drawing.Size(104, 24);
			this.cancelButton.TabIndex = 20;
			this.cancelButton.Text = "Cancel";
			this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
			// 
			// addButton
			// 
			this.addButton.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(224)), ((System.Byte)(223)), ((System.Byte)(227)));
			this.addButton.DefaultScheme = true;
			this.addButton.DialogResult = System.Windows.Forms.DialogResult.None;
			this.addButton.Hint = "";
			this.addButton.Location = new System.Drawing.Point(37, 152);
			this.addButton.Name = "addButton";
			this.addButton.Scheme = PinkieControls.ButtonXP.Schemes.Silver;
			this.addButton.Size = new System.Drawing.Size(104, 24);
			this.addButton.TabIndex = 19;
			this.addButton.Text = "Add";
			this.addButton.Click += new System.EventHandler(this.addButton_Click);
			// 
			// usernameField
			// 
			this.usernameField.Location = new System.Drawing.Point(112, 32);
			this.usernameField.Name = "usernameField";
			this.usernameField.Size = new System.Drawing.Size(152, 20);
			this.usernameField.TabIndex = 1;
			this.usernameField.Text = "";
			// 
			// NewUser
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(290, 192);
			this.Controls.Add(this.usernameField);
			this.Controls.Add(this.cancelButton);
			this.Controls.Add(this.addButton);
			this.Controls.Add(this.confirmPasswordField);
			this.Controls.Add(this.passwordField);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "NewUser";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "New user";
			this.ResumeLayout(false);

		}
		#endregion


		private void cancelButton_Click(object sender, System.EventArgs e)
		{
			this.Dispose();
		}

		private void addButton_Click(object sender, System.EventArgs e)
		{
            createUser();
				
		}

		private void Enter_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			// If enter is pressed
			if (e.KeyCode == Keys.Enter)
			{
                createUser();
			}
		}

        private void createUser()
        {
            string[] usernames;

            try
            {
                if (passwordField.Text != confirmPasswordField.Text)
                {
                    MessageBox.Show("Passwords does not match.", "Invalid Password", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.Dispose();
                }
                else
                {
                    newUser.Name = usernameField.Text;
                    newUser.Password = passwordField.Text;
                    manager.AddNewUser(newUser);
                    this.Dispose();
                }
            }
            catch (MizExceptions.UserAlreadyExistsException e1)
            {
                MessageBox.Show(e1.Message, "User already exists", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (MizExceptions.ServerUnavailableException)
            {
                this.Dispose();
            }
        }

	}
}
